//#Author:bin
// -*- charset = utf-8 -*-
// @time:2021/12/17 16:30
// @Author:bfl91
// @file : cart.js
// @Software:WebStorm
export default {
    namespaced:true,
    state:{
        cartList:[],//购物车列表
    },
    getters:{
        shopAtter(state){
             let number=0//购物车商品的数量
             let AllCkecked=true;//全选按钮
             let CkeckedPrice=0;//选中状态下的总价
             let CkeckedLiat=[];//加入订单的商品
            state.cartList.forEach(v=>{
                if(!v.ShopSelect){AllCkecked=false}//全选按钮的状态
                number+=v.selectedNum//数量
                if(v.ShopSelect){
                    CkeckedPrice+=v.selectedSkuComb.price*v.selectedNum//选中的总价
                    CkeckedLiat.push(v)
                }
            })
            if(state.cartList.length==0){AllCkecked=false}//当购物车数据没有了全选按钮的状态
             return {number,AllCkecked,CkeckedPrice,CkeckedLiat}
            localStorage.setItem('cartList',JSON.stringify(state.cartList))
        }
    },
    mutations: {
        //全选按钮控制子按钮
        setAllCkecked(state, bool) {
            state.cartList.forEach(v => {
                v.ShopSelect = bool;
            })
        },
        //删除商品
        removeShop(state, index) {
            state.cartList.splice(index, 1)
        },
        //删除加入订单后的商品
        removeTrue(state){
            state.cartList.forEach((v,i)=>{
                if(v.ShopSelect){
                    state.cartList.splice(i,1)
                }
            })
            },
        //添加到购物车
        setList(state,obj) {
            let list=state.cartList;
            for(var i=0;i<list.length;i++){
                if(list[i].goodsId==obj.goodsId){
                    list[i].selectedNum+=obj.selectedNum;
                    return false;
                }
            }
            state.cartList.push(obj)
        },
        //修改商品数量
        setNumber(state,obj) {
            console.log(obj)
            let {index,num}=obj;
            let number=state.cartList[index].selectedNum+num;
            if(number>0){
                state.cartList[index].selectedNum+=num;
            }

        }
    },
}